<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>


        function* gene() {
            let result1 = yield 1
            console.log(result1);
            let result2 = yield 2
            console.log(result2);
            let result3 = yield 3
            console.log(result3);
            let result4 = yield timeout(2000)
            console.log(result4);
            return 5
        }

        function co(gene) {
            const g = gene()
            function run(prvValue) {
                let { value, done } = g.next(preValue)
                if (!done) {
                    if (typeof value.then === 'function' && typeof value.catch === 'function') {
                        value.then(res => {
                            run(res)
                        })
                    } else {
                        run(value)
                    }
                }
            }
            run()
        }
        co(gene)
    </script>
</body>

</html>